package com.example.springbootdesign.controller;

import com.example.springbootdesign.config.ProductStrategyFactory;
import com.example.springbootdesign.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ProductController {

    @Resource
    private ProductStrategyFactory factoryForStrategy;

    @Autowired
    private OrderService orderServiceA;
    @Autowired
    private OrderService orderServiceB;
    @Autowired
    private OrderService orderServiceC;

    @RequestMapping("/order")
    public String order(@RequestParam(value = "type") String type) {
        String product = "";
        if ("productA".equals(type)) {
            product= orderServiceA.orderingProduct();
        } else if ("productB".equals(type)) {
            product =orderServiceB.orderingProduct();
        } else if ("productC".equals(type)) {
            product =orderServiceC.orderingProduct();
        }
        System.out.println(product);
        // 没有对应的产品,抛出异常并返回null
        // throw new RuntimeException("没有发现对应的产品处理策略");
        return product;
    }

    @RequestMapping("/order2")
    public String order2(@RequestParam(value = "type") String type) {
       OrderService productService = factoryForStrategy.getProductStrategy(type);
        return productService != null ? productService.orderingProduct() : "没有发现对应的产品处理策略";
    }

}
